package whu.iss.web.sys;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import whu.iss.pojo.UserCustom;
import whu.iss.services.impl.sys.RegisterServices;
import whu.iss.system.tools.Tools;


/**
 * Servlet implementation class LoginServlet
 */
@SuppressWarnings("serial")
@WebServlet(value="/register")
public class RegisterServlet extends HttpServlet
{
		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
		{
			String toPath="register.jsp";
			try
			{
				//1.获取页面数据
				String lname=request.getParameter("lname");
				String pwd1=request.getParameter("pwd1");
				String pwd2=request.getParameter("pwd2");
				String message=null;
				
				RegisterServices services=new RegisterServices();
				UserCustom usercustom=new UserCustom();
				boolean isExist=services.isUsernameExist(lname);
				
				if(isExist)
				{
					message="用户名已存在!";			
				}
				else if(!pwd1.equals(pwd2))
				{
					message="两次输入的密码不一样!";
				}
				else if(lname.equals("")||lname==null)
				{
					message="用户名不能为空！";
				}
				else
				{
					usercustom.setUsername(lname);
					usercustom.setPwd(Tools.getMd5(pwd1));
					services.insertUser(usercustom);
					toPath="login.jsp";
				}
				request.getSession().setAttribute("message", message);
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
			request.getRequestDispatcher("/"+toPath).forward(request, response);
		}

		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
		{
			this.doGet(request, response);
		}

	
}
